package com.niuzi.filter;

import com.niuzi.entity.Admin;
import com.niuzi.util.PropertiesUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Administrator
 */
@WebFilter(urlPatterns = "/*")
public class Filter010Encoding implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String uri = req.getRequestURI();
        System.out.println("过滤器："+uri);
        Admin admin = (Admin) req.getSession().getAttribute("nologin");
        if(admin!=null){
            filterChain.doFilter(req,resp);
        }else{
            String nologin = PropertiesUtil.getPrperties("nologin");
            if(nologin != null){
                String[] res = nologin.split("\\|");
                for (String re : res) {
                    if(uri.endsWith(re)){
                        filterChain.doFilter(req,resp);
                        return;
                    }
                }
            }
            resp.sendRedirect("login.jsp");
        }
    }

    @Override
    public void destroy() {

    }
}
